package com.edu.cs576.playerWithHyperLink;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class ReadMetaFile {
	
	ArrayList Rects = new ArrayList();
	
	public ReadMetaFile() throws FileNotFoundException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("vdo1.meta")));  
		String data = null; 
		String tempStringAry[];
		
		
		ArrayList KeyFrames = new ArrayList();
		ArrayList KeyFramesInfo = new ArrayList();
		String[] KeyFrameInfo;
	    ArrayList RectsInfo = new ArrayList();
	    ArrayList RectAttribures = new ArrayList();
	    


	    
	
		
		int NumberOfRects = 0;
		int NumberOfKeyFrames = 0;
		int CurrentLine = 1;
		
		System.out.println("Reading!");
		try {
				
				//Read out [rect1ID, rect1Name, numOfKeyFrames, linkedVideoName, linkedVideoStartFrameNo]
				while((data = br.readLine())!=null)  
				{  					
					System.out.println("Line "+CurrentLine);  


					tempStringAry = data.split(" ");
					//System.out.println(tempStringAry.length); //3,2,5,5
					
					
					
					if(tempStringAry.length == 3)
					{
						//System.out.println("First");
						RectAttribures.add(tempStringAry[0]);
						RectAttribures.add(tempStringAry[1]);
						RectAttribures.add(tempStringAry[2]);
						//for(int i = 0 ; i < RectAttribures.size() ; i++)
							//System.out.println(RectAttribures.get(i));
						//for(int i = 0 ; i < tempStringAry.length ; i++)
							//System.out.println(tempStringAry[i]);
					}
					else if(tempStringAry.length == 2)
					{
						RectAttribures.add(tempStringAry[0]);
						RectAttribures.add(tempStringAry[1]);
						if(RectAttribures.size() != 0)
							RectsInfo.add(RectAttribures);
					}

					
					System.out.println(RectsInfo);
					
					for(int j = 0 ; j < RectAttribures.size() ; j++)
						System.out.println(RectAttribures.get(j));
					
					if(data.contains(".rgb"))
					{
						//System.out.println(data.substring(0, data.indexOf(".rgb")+4));
						NumberOfRects++;
						RectAttribures = new ArrayList();
					}
	
					
					//System.out.println(RectAttribures.size());

						
					CurrentLine++;
				}
			
				System.out.println("RectsInfo.size() " + RectsInfo.size());
				System.out.println(RectsInfo);
				
				/*
				for(int i = 0 ; i < RectsInfo.size() ; i++)
				{
					ArrayList tempAttribute = (ArrayList) RectsInfo.get(i);
					System.out.println();
					System.out.println("RectInfo:");
					for(int j = 0 ; j < tempAttribute.size() ; j++)
						System.out.println(tempAttribute.get(j));
					
				}
				
				*/
				br.close();
				
				
				//KeyFrames = new String[NumberOfRects][][5];
				KeyFrameInfo = new String[5];
				
				
				
				br = new BufferedReader(new InputStreamReader(new FileInputStream("vdo1.meta")));  
				while((data = br.readLine())!=null)  
				{  
					System.out.println("Line "+CurrentLine); 
					
					tempStringAry = data.split(" ");
										
					//System.out.println(data);  

					
					if(tempStringAry.length == 5)
					{
						/*
						KeyFrameInfo = new String[5];
						KeyFrameInfo[0] = (tempStringAry[0]);
						KeyFrameInfo[1] = (tempStringAry[1]);
						KeyFrameInfo[2] = (tempStringAry[2]);
						KeyFrameInfo[3] = (tempStringAry[3]);
						KeyFrameInfo[4] = (tempStringAry[4]);
				
						KeyFrames.add(KeyFrameInfo);
						*/
						KeyFramesInfo = new ArrayList();
						KeyFramesInfo.add(tempStringAry[0]);
						KeyFramesInfo.add(tempStringAry[1]);
						KeyFramesInfo.add(tempStringAry[2]);
						KeyFramesInfo.add(tempStringAry[3]);
						KeyFramesInfo.add(tempStringAry[4]);
						KeyFrames.add(KeyFramesInfo);
						
						
					}
					
					if(data.contains(".rgb"))
					{
						if(KeyFrames.size() > 0)
						{
							Rects.add(KeyFrames);
							KeyFrames = new ArrayList();		
						}
					}
					System.out.println(Rects);
					
					
					
					CurrentLine++;
				}
				br.close();
				
				Rects.add(KeyFrames);
				System.out.println("NumberOfRects: " +NumberOfRects);
				//System.out.println("Rects.size() "+Rects.size());
				System.out.println(Rects);
				
				

				
				
				

				

				
					//testing only
					//ArrayList Rectanle = new ArrayList();
					//Rectanle = (ArrayList) Rects.get(2);
					//ReturnValue.add(CWERI(Rectanle));

				
		
			
		} catch (IOException e) {
			System.out.println("File Not Found!");  
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public ArrayList getAllRectsToDraw()
	{
	    ArrayList AllRects = new ArrayList();
		for(int i = 0 ; i < Rects.size() ; i++)
		{
			ArrayList Rectanle = new ArrayList();
			Rectanle = (ArrayList) Rects.get(i);
			AllRects.add(CWERI(Rectanle));
			
		}
		return AllRects;
	}
	
	
	public ArrayList getAllStartandEndFrameNum()
	{
	    ArrayList AllStartEndFrame = new ArrayList();
		for(int i = 0 ; i < Rects.size() ; i++)
		{
			ArrayList Rectangle = new ArrayList();
			ArrayList FirstKeyFrame = new ArrayList();
			ArrayList LastKeyFrame = new ArrayList();	
			ArrayList StartandEnd = new ArrayList();
			
			Rectangle = (ArrayList) Rects.get(i);
			FirstKeyFrame = (ArrayList) Rectangle.get(0);
			LastKeyFrame = (ArrayList) Rectangle.get(Rectangle.size()-1);
			
			int startFrame = Integer.parseInt((String) FirstKeyFrame.get(0));
			int EndFrame = Integer.parseInt((String) LastKeyFrame.get(0));
			StartandEnd.add(startFrame);
			StartandEnd.add(EndFrame);
			AllStartEndFrame.add(StartandEnd);

		}
		
		System.out.println("AllStartEndFrame: " + AllStartEndFrame);
		return AllStartEndFrame;
	}
	
	public ArrayList CWERI(ArrayList RectangleKeyFrames)
	{
		int numOfKeyFrames = RectangleKeyFrames.size();
		
		ArrayList rects = new ArrayList();
		for (int k = 0 ; k < RectangleKeyFrames.size()-1 ; k++ )
		{	
			ArrayList FirstKeyFrame = new ArrayList();
			FirstKeyFrame = (ArrayList) RectangleKeyFrames.get(k);
		
			ArrayList SecondKeyFrame = new ArrayList();
			SecondKeyFrame = (ArrayList) RectangleKeyFrames.get(k+1);
		

			int numOfInterpolateFrames = Integer.parseInt((String) SecondKeyFrame.get(0))  - Integer.parseInt((String) FirstKeyFrame.get(0));
			System.out.println("numOfInterpolateFrames: " + numOfInterpolateFrames);
		
			float x1 = Float.parseFloat((String)FirstKeyFrame.get(1));
			System.out.println("x1: " + x1);
			float y1 = Float.parseFloat((String)FirstKeyFrame.get(2));
			System.out.println("y1: " + y1);
			float x2 = Float.parseFloat((String)FirstKeyFrame.get(3));
			System.out.println("x2: " + x2);
			float y2 = Float.parseFloat((String)FirstKeyFrame.get(4));
			System.out.println("y2: " + y2);
		
			float x1Augment = (Float.parseFloat((String)SecondKeyFrame.get(1)) - Float.parseFloat((String)FirstKeyFrame.get(1))) / numOfInterpolateFrames ;
			System.out.println("x1Augment: " + x1Augment);
		
			float y1Augment = (Float.parseFloat((String)SecondKeyFrame.get(2)) - Float.parseFloat((String)FirstKeyFrame.get(2))) / numOfInterpolateFrames ;
			System.out.println("x2Augment: " + y1Augment);
				
			float x2Augment = (Float.parseFloat((String)SecondKeyFrame.get(3)) - Float.parseFloat((String)FirstKeyFrame.get(3))) / numOfInterpolateFrames ;
			System.out.println("y1Augment: " + x2Augment);
		
			float y2Augment = (Float.parseFloat((String)SecondKeyFrame.get(4)) - Float.parseFloat((String)FirstKeyFrame.get(4))) / numOfInterpolateFrames ;
			System.out.println("y2Augment: " + y2Augment);
		
			for(int i = 0 ; i < numOfInterpolateFrames+1 ; i++)
			{
				ArrayList pointsForDrawingRects = new ArrayList();
				pointsForDrawingRects.add( Math.round( ( x1 + ( x1Augment * i) ) ));
				//System.out.println("pointsForDrawingRects[0]: " + pointsForDrawingRects[0]);
				pointsForDrawingRects.add( Math.round( ( y1 + ( y1Augment * i) ) ));
				//System.out.println("pointsForDrawingRects[1]: " + pointsForDrawingRects[1]);
				pointsForDrawingRects.add(  Math.round( ( x2 + ( x2Augment * i) ) - (x1 + (x1Augment*i)) ));
				//System.out.println("pointsForDrawingRects[2]: " + pointsForDrawingRects[2]);
				pointsForDrawingRects.add( Math.round( ( y2 + ( y2Augment * i) ) - (y1 + (y1Augment*i)) ));
				//System.out.println("pointsForDrawingRects[3]: " + pointsForDrawingRects[3]);
				rects.add(pointsForDrawingRects);
				
			}
			
		}
		
		System.out.println("rectanglePoints: " + rects);
		
		/*
		int test[] = new int[4]; 
		for(int i = 0 ; i < rectanglePoints.size() ; i++)
		{
			System.out.println("value: ");
			System.out.println("");
			test = (int[]) rectanglePoints.get(i);
			for(int j = 0 ; j < 4 ; j++)
			{
				System.out.println(test[j]);
			}
		}*/
		return rects;
		
		
		
		
		
		
		/*  
		 * when playing Vdo1 each frame check if contains below, if does 
		 * [StartFrame#1 , StartFrame#2, StartFrame#3]
		 * 
		 * 
		 * Draw Rectangles from
		 * 
		 * [ Rect1                                                              ] 
		 * [ StartFrame#1                                                       ]
		 * [ [ [x1,y1,x2-x1,y2-y1], [x1+Augment,y1+Augment,(x2+Augment)-(x1+Augment),(y2+Augment)-(y1+Augment)], [x1,y1,x2-x1,y2-y1] ]  ]
		 * [                                                                    ]
		 * [ Rect2                                                              ]
		 * [ StartFrame#2                                                       ]
		 * [ [ [x1,y1,x2-x1,y2-y1], [x1,y1,x2-x1,y2-y1], [x1,y1,x2-x1,y2-y1] ]  ]
		 * [                                                                    ]
		 * [ Rect3                                                              ]
		 * [ StartFrame#3                                                       ]
		 * [ [ [x1,y1,x2-x1,y2-y1], [x1,y1,x2-x1,y2-y1], [x1,y1,x2-x1,y2-y1] ]  ]
		 * [                                                                    ]
		 */

	}

}
